﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Asteroids
{
    class Ship
    {
        public Texture2D  tex_ship;
        public Vector2    pos_ship;

        public float     angle_direct;
        public float     radians_direct;
        public int       vida = 100;
        private int      speed;
        private int      gravidade = 2;
        private bool     voando = true;
        private bool     olhandoDireita = true;
              
        public Ship(Texture2D texship, Vector2 posship, int speed)
        {
            this.pos_ship = posship;
            this.tex_ship = texship;
            this.speed = 5;            
        }

        public void Draw(SpriteBatch spriteBratch, Vector2 cameraorigem)
        {
            spriteBratch.Begin();
            spriteBratch.Draw(tex_ship, new Rectangle(((int)cameraorigem.X + (int)pos_ship.X), ((int)cameraorigem.Y + (int)pos_ship.Y), (int)tex_ship.Width, (int)tex_ship.Height),
                new Rectangle(0, 0, tex_ship.Width, tex_ship.Height),
                Color.White, radians_direct,
                new Vector2(tex_ship.Width/2, tex_ship.Height/2), 
                SpriteEffects.None, 0f);
            spriteBratch.End();
        }

        public void Move(KeyboardState kAtual, Keys cima, Keys baixo, Keys esquerda, Keys direita)
        {
            radians_direct = MathHelper.ToRadians(angle_direct);

            if (kAtual.IsKeyDown(cima))
            {
                if (speed < 100)
                { speed++; }
                else 
                {}
                pos_ship += new Vector2(((float)Math.Cos(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90))) * (speed/10)), ((float)Math.Sin(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90)))) * (speed/10));
            }
            else if (kAtual.IsKeyUp(cima))
            {
                if (speed <= 100 && speed > 0)
                {
                    speed--;
                    pos_ship += new Vector2(((float)Math.Cos(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90))) * (speed / 10)), ((float)Math.Sin(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90)))) * (speed / 10));
                }
            }
            if (kAtual.IsKeyDown(baixo))
            {
                if (olhandoDireita)
                {

                }
                //pos_ship -= new Vector2((float)Math.Cos(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90))), (float)Math.Sin(Convert.ToDouble(MathHelper.ToRadians(angle_direct - 90))));
            }
            if (kAtual.IsKeyDown(esquerda))
            {
                angle_direct -= 2;
            }
            if (kAtual.IsKeyDown(direita))
            {
                angle_direct += 2;
            }
            if (!voando)
            {
                pos_ship.Y += gravidade;
            }
        }
    }
}
